﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using Dictionary.ERP;
using Common;
using Common.Class;
using DevExpress.XtraEditors.Mask;
using Security.ERP;
using Extra.ERP;
using System.Data;

namespace Dictionary
{
    public class xucStepAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, DIC_SALARY_STEP item);

        private readonly IContainer components = null;
        private CalcEdit calCoefficient;
        private GridLookUpEdit glkRank;
        private GridColumn gridColumn3;
        private GridColumn gridColumn4;
        private GridView gridLookUpEdit1View;
        private LabelControl lcBranch;
        private LabelControl lcCoefficient;
        private LabelControl lcDescription;
        private TextEdit txtDescription;

        public xucStepAdd()
        {
            InitializeComponent();
            InitData();
            calCoefficient.EditValue = 0.0;
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            try
            {
                var dic_salary_step = new DIC_SALARY_STEP();
                SetID(dic_salary_step.NewID(glkRank.EditValue.ToString()));
            }
            catch
            {
            }
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            calCoefficient.EditValue = 0.0;
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void glkRank_EditValueChanged(object sender, EventArgs e)
        {
            if (base.txtID.ErrorText != string.Empty)
            {
                base.Err.SetError(base.txtID, string.Empty);
            }
            if (base.m_Status == Actions.Add)
            {
                var dic_salary_step = new DIC_SALARY_STEP();
                SetID(dic_salary_step.NewID(glkRank.EditValue.ToString()));
                if (dic_salary_step.Exist(base.txtID.Text, glkRank.EditValue.ToString()))
                {
                    base.Err.SetError(base.txtID, "M\x00e3 đ\x00e3 tồn tại.");
                    base.txtID.Focus();
                }
            }
        }

        protected override void Init()
        {
        }

        private void InitData()
        {
            DataTable list = new DIC_SALARY_RANK().GetList();
            glkRank.Properties.DataSource = list;
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucStepAdd));
            glkRank = new GridLookUpEdit();
            gridLookUpEdit1View = new GridView();
            gridColumn3 = new GridColumn();
            gridColumn4 = new GridColumn();
            lcBranch = new LabelControl();
            lcCoefficient = new LabelControl();
            txtDescription = new TextEdit();
            lcDescription = new LabelControl();
            calCoefficient = new CalcEdit();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            glkRank.Properties.BeginInit();
            gridLookUpEdit1View.BeginInit();
            txtDescription.Properties.BeginInit();
            calCoefficient.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(calCoefficient);
            base.gcInfo.Controls.Add(glkRank);
            base.gcInfo.Controls.Add(lcBranch);
            base.gcInfo.Controls.Add(lcCoefficient);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Controls.Add(lcDescription);
            base.gcInfo.Size = new Size(0x14c, 160);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(lcDescription, 0);
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(lcCoefficient, 0);
            base.gcInfo.Controls.SetChildIndex(lcBranch, 0);
            base.gcInfo.Controls.SetChildIndex(glkRank, 0);
            base.gcInfo.Controls.SetChildIndex(calCoefficient, 0);
            base.txtNAME.Location = new Point(0x5f, 0x34);
            base.txtNAME.Size = new Size(0xe5, 20);
            base.lblName.Location = new Point(6, 0x37);
            base.txtID.Location = new Point(0x5f, 0x1a);
            base.txtID.Properties.EditFormat.FormatType = FormatType.Numeric;
            base.txtID.Properties.Mask.MaskType = MaskType.Numeric;
            base.txtID.Size = new Size(0xe5, 20);
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x27, 0xaf);
            base.btnCancel.Location = new Point(230, 0xaf);
            base.btnSaveNew.Location = new Point(0x89, 0xaf);
            glkRank.Location = new Point(0x5f, 0x4e);
            glkRank.Name = "glkRank";
            glkRank.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            glkRank.Properties.DisplayMember = "RankName";
            glkRank.Properties.NullText = "[Chọn ngạch lương]";
            glkRank.Properties.ValueMember = "RankCode";
            glkRank.Properties.View = gridLookUpEdit1View;
            glkRank.Size = new Size(0xe5, 20);
            glkRank.TabIndex = 0x24;
            glkRank.EditValueChanged += glkRank_EditValueChanged;
            gridLookUpEdit1View.Columns.AddRange(new[] {gridColumn3, gridColumn4});
            gridLookUpEdit1View.FocusRectStyle = DrawFocusRectStyle.RowFocus;
            gridLookUpEdit1View.Name = "gridLookUpEdit1View";
            gridLookUpEdit1View.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridLookUpEdit1View.OptionsView.ShowGroupPanel = false;
            gridColumn3.Caption = "M\x00e3 ngạch lương";
            gridColumn3.FieldName = "RankCode";
            gridColumn3.Name = "gridColumn3";
            gridColumn3.Visible = true;
            gridColumn3.VisibleIndex = 0;
            gridColumn4.Caption = "T\x00ean ngạch lương";
            gridColumn4.FieldName = "RankName";
            gridColumn4.Name = "gridColumn4";
            gridColumn4.Visible = true;
            gridColumn4.VisibleIndex = 1;
            lcBranch.Location = new Point(7, 80);
            lcBranch.Name = "lcBranch";
            lcBranch.Size = new Size(80, 13);
            lcBranch.TabIndex = 0x23;
            lcBranch.Text = "Với ngạch lương:";
            lcCoefficient.Location = new Point(6, 0x6b);
            lcCoefficient.Name = "lcCoefficient";
            lcCoefficient.Size = new Size(0x3d, 13);
            lcCoefficient.TabIndex = 0x22;
            lcCoefficient.Text = "Hệ số lương:";
            txtDescription.Location = new Point(0x5f, 0x83);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0xe5, 20);
            txtDescription.TabIndex = 0x1f;
            lcDescription.Location = new Point(6, 0x86);
            lcDescription.Name = "lcDescription";
            lcDescription.Size = new Size(0x27, 13);
            lcDescription.TabIndex = 0x20;
            lcDescription.Text = "Ghi ch\x00fa:";
            calCoefficient.Location = new Point(0x5f, 0x68);
            calCoefficient.Name = "calCoefficient";
            calCoefficient.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            calCoefficient.Size = new Size(0xe5, 20);
            calCoefficient.TabIndex = 0x25;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucStepAdd";
            base.Size = new Size(0x14d, 0xd8);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            glkRank.Properties.EndInit();
            gridLookUpEdit1View.EndInit();
            txtDescription.Properties.EndInit();
            calCoefficient.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void RaiseSuccessEventHander(DIC_SALARY_STEP item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(DIC_SALARY_STEP item)
        {
            base.txtID.Text = item.StepCode.ToString();
            glkRank.EditValue = item.RankCode;
            SYS_LOG.Insert("Danh Mục Bậc Lương", "Xem", base.txtID.Text);
            if (base.m_Status == Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
                glkRank.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.StepName;
            calCoefficient.Text = item.Coefficient.ToString();
            txtDescription.Text = item.Description;
        }

        public void SetID(string id)
        {
            base.txtID.Text = Convert.ToInt32(id).ToString();
            base.txtNAME.Text = "Bậc " + Convert.ToInt32(id);
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            if (base.m_Status == Actions.Add)
            {
                var dic_salary_step = new DIC_SALARY_STEP();
                if ((glkRank.EditValue != null) && dic_salary_step.Exist(control.Text, glkRank.EditValue.ToString()))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var dic_salary_step = new DIC_SALARY_STEP();
                if ((base.m_Status == Actions.Add) && dic_salary_step.Exist(control.Text, glkRank.EditValue.ToString()))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new DIC_SALARY_STEP
            {
                StepCode = int.Parse(base.txtID.Text)
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiStep") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            if (glkRank.EditValue == null)
            {
                XtraMessageBox.Show("Vui l\x00f2ng chọn ngạch lương tương ứng!", "Cảnh B\x00e1o", MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);
                return "";
            }
            SYS_LOG.Insert("Danh Mục Bậc Lương", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            var item = new DIC_SALARY_STEP(int.Parse(base.txtID.Text), glkRank.EditValue.ToString(), base.txtNAME.Text,
                double.Parse(calCoefficient.Text), txtDescription.Text);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiStep") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh Mục Bậc Lương", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var item = new DIC_SALARY_STEP(int.Parse(base.txtID.Text), glkRank.EditValue.ToString(), base.txtNAME.Text,
                double.Parse(calCoefficient.Text), txtDescription.Text);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }
    }
}